﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using VideoClipNameHelper;

namespace Videos2Links
{
    partial class Program
    {
        //=========================================================================================================================================
        private static bool createLinkByAll(string dstDir, string linkName, VideoFile vf)
        {
            if (IsWithoutWriting) { return true; }
            //
            try
            {
                if (!Directory.Exists(dstDir))
                {
                    Directory.CreateDirectory(dstDir);
                }
                //
                string lnkPathNameWithoutExt = Path.Combine(dstDir, linkName);
                //
                //ShellLink.Create(lnkPathName, vf.PathName);
                createLink(lnkPathNameWithoutExt, vf.PathName);
            }
            catch (Exception e)
            {
                string eMsg = string.Format($"ERROR createLinkByAll([{dstDir}][{linkName}][{vf.PathName}]) {e.Message},{e.Source},{e.InnerException}");
                errList.Add(eMsg);
                CLogger.Write(eMsg);
                Console.WriteLine(eMsg);
            }
            //
            return true;
        }
        //-----------------------------------------------------------------------------------------------------------------------------------------
        private static bool createLinkByArtist(string dstDir, VideoFile vf)
        {
            if (IsWithoutWriting) { return true; }
            //
            string dstKeyDir="";
            string lnkPathNameWithoutExt = "";
            try
            {
                if ("" == vf.vcd.Title)
                {
                    dstKeyDir = Path.Combine(dstDir, vf.vcd.Artist);
                }
                else
                {
                    dstKeyDir = Path.Combine(dstDir, vf.vcd.Artist + " " + vf.vcd.Title);
                }
                if (!Directory.Exists(dstKeyDir))
                {
                    Directory.CreateDirectory(dstKeyDir);
                }
                //
                //string lnkPathName = Path.Combine(dstKeyDir, vf.vcd.Artist + " - " + vf.vcd.Title);
                //string lnkPathName = Path.Combine(dstKeyDir, vf.vcd.Artist + " - " + vf.vcd.Concat()) + ".lnk";
                lnkPathNameWithoutExt = Path.Combine(dstKeyDir, vf.FileNameWithoutExtension());
                //
                createLink(lnkPathNameWithoutExt, vf.PathName);
                //
            }
            catch (Exception e)
            {
                string eMsg = string.Format($"ERROR createLinkByArtist([{dstKeyDir}][{lnkPathNameWithoutExt}][{vf.PathName}]) {e.Message},{e.Source},{e.InnerException}");
                errList.Add(eMsg);
                CLogger.Write(eMsg);
                Console.WriteLine(eMsg);
            }
            //
            return true;
        }
        //-----------------------------------------------------------------------------------------------------------------------------------------
        private static bool createLinksByArtist(string dstDir, IEnumerable<VideoFile> vfs)
        {
            if (IsWithoutWriting) { return true; }
            //
            string dstKeyDir="";
            try
            {
                dstKeyDir = Path.Combine(cp.ByArtistDir, vfs.First().vcd.Artist);
                if (!Directory.Exists(dstKeyDir))
                {
                    Directory.CreateDirectory(dstKeyDir);
                }
                //

                //Parallel.ForEach(vfs, (vf) =>
                foreach (VideoFile vf in vfs)
                {
                    //string lnkPathName = Path.Combine(dstKeyDir, vf.vcd.Artist + " - " + vf.vcd.Title);
                    //string lnkPathName = Path.Combine(dstKeyDir, vf.vcd.Artist + " - " + vf.vcd.Concat()) + ".lnk";
                    string lnkPathNameWithoutExt = Path.Combine(dstKeyDir, vf.FileNameWithoutExtension());
                    //
                    createLink(lnkPathNameWithoutExt, vf.PathName);
                    //
                    }
                //});
                if (vfs.Count() > 0)
                {
                    bool isOrdinal = vfs.Count(x => x.vcd.Ordinal != VideoClipData.UndefinedOrdinal) > 0;
                    // jeżeli jest Ordinal to odtwarzamy kolejno, po Ordinalu
                    if (isOrdinal)
                    {
                        _WriteHintFile(dstKeyDir, false);
                    }
                    else
                    {
                        _WriteHintFile(dstKeyDir, true);
                    }
                }
            }
            catch (Exception e)
            {
                string eMsg = string.Format($"ERROR createLinkByArtist([{dstKeyDir}]) {e.Message},{e.Source},{e.InnerException}");
                errList.Add(eMsg);
                CLogger.Write(eMsg);
                Console.WriteLine(eMsg);
            }
            //
            return true;
        }
        //-----------------------------------------------------------------------------------------------------------------------------------------
        private static bool createLinksByAlbum(string dstDir, IEnumerable<VideoFile> vfs)
        {
            if (IsWithoutWriting) { return true; }
            //
            string dstKeyDir="";
            try
            {
                dstKeyDir = Path.Combine(cp.ByAlbumDir, vfs.First().vcd.Artist + " ／ " + vfs.First().vcd.Album);
                if (!Directory.Exists(dstKeyDir))
                {
                    Directory.CreateDirectory(dstKeyDir);
                }
                //
                //Parallel.ForEach(vfs, (vf) =>
                foreach (VideoFile vf in vfs)
                {
                    //string lnkPathName = Path.Combine(dstKeyDir, vf.vcd.Artist + " - " + vf.vcd.Title);
                    //string lnkPathName = Path.Combine(dstKeyDir, vf.vcd.Artist + " - " + vf.vcd.Concat()) + ".lnk";
                    string lnkPathNameWithoutExt = Path.Combine(dstKeyDir, vf.FileNameWithoutExtension());
                    //
                    createLink(lnkPathNameWithoutExt, vf.PathName);
                    //
                }
                //});
                if (vfs.Count() > 0)
                {
                    bool isOrdinal = vfs.Count(x => x.vcd.Ordinal != VideoClipData.UndefinedOrdinal) > 0;
                    // jeżeli jest Ordinal to odtwarzamy kolejno, po Ordinalu
                    if (isOrdinal)
                    {
                        _WriteHintFile(dstKeyDir, false);
                    }
                    else
                    {
                        _WriteHintFile(dstKeyDir, true);
                    }
                }

            }
            catch (Exception e)
            {
                string eMsg = string.Format($"ERROR createLinkByArtist([{dstKeyDir}]) {e.Message},{e.Source},{e.InnerException}");
                errList.Add(eMsg);
                CLogger.Write(eMsg);
                Console.WriteLine(eMsg);
            }
            //
            return true;
        }
        //-----------------------------------------------------------------------------------------------------------------------------------------
        private static bool createLinksByTitle(string dstDir, IEnumerable<VideoFile> vfs)
        {
            if (IsWithoutWriting) { return true; }
            //
            string dstKeyDir = "";
            try
            {
                dstKeyDir = Path.Combine(dstDir, vfs.First().vcd.Title);
                if (!Directory.Exists(dstKeyDir))
                {
                    Directory.CreateDirectory(dstKeyDir);
                }
                //Parallel.ForEach(vfs, (vf) =>
                foreach (VideoFile vf in vfs)
                {
                    string lnkPathNameWithoutExt = Path.Combine(dstKeyDir, vf.FileNameWithoutExtension());
                    //
                    createLink(lnkPathNameWithoutExt, vf.PathName);
                };
                //});
                if (vfs.Count() > 0)
                {
                    _WriteHintFile(dstKeyDir, true);
                }
            }
            catch (Exception e)
            {
                string eMsg = string.Format($"ERROR createLinksByTitle([{dstKeyDir}]) {e.Message},{e.Source},{e.InnerException}");
                errList.Add(eMsg);
                CLogger.Write(eMsg);
                Console.WriteLine(eMsg);
            }
            //
            return true;
        }
        //=========================================================================================================================================
        private static object o = new object();
        /// <summary>
        /// Tworzy shortcut w podanym miejscu i o podanej nazwie
        /// do podanego pliku
        /// </summary>
        /// <param name="linkPathNameWithoutExt">ścieżka do pliku shortcut</param>
        /// <param name="targetFileName">ścieżka do docelowy plik muzyczny</param>
        private static void createLink(string linkPathNameWithoutExt, string targetFileName)
        {
            lock (o)
            {
                string lnkPathName = linkPathNameWithoutExt + ".lnk";
                //
                if (System.IO.File.Exists(lnkPathName))
                {
                    System.IO.File.Delete(lnkPathName);
                }
                //
                //ShellLink.Create(lnkPathName, vf.PathName);
                VideoClipNameHelper.Shortcut link = new VideoClipNameHelper.Shortcut();
                link.Path = targetFileName;
                link.Description = System.IO.Path.GetFileNameWithoutExtension(targetFileName);
                link.Save(lnkPathName);
                //
            }
        }
        //=========================================================================================================================================
    }//class
}//namespace
